<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title">从事专业</h4>
</div>

<div class="modal-body">
    <div class="row">
        <div class="col-sm-6">
            <div class="input-group">
                <input type="text" class="form-control input-sm" id="input_search">
                <div class="input-group-btn">
                    <button class="btn btn-default btn-sm  btn-success" id="btn_search" type="button"><i
                            class="glyphicon glyphicon-search"></i>&nbsp;搜&nbsp;索
                    </button>
                </div>
            </div>
        </div>
        <div class="col-sm-3 col-sm-offset-3">
            <button id="btn_save_data" class="btn-success btn-default btn btn-sm"><i
                    class="glyphicon glyphicon-saved"></i> 保存
            </button>
        </div>
        <div class="col-sm-12">
            <div id="div_error"></div>
        </div>
        <div class="col-sm-12">
            <div class="form-control-free" style="overflow-y:auto; overflow-x:auto ;max-height: 300px;">
                <ul id="tree_data" class="ztree col-sm-10 col-sm-offset-2"></ul>
            </div>
        </div>
    </div>
</div>

<script>
    var lastValue = "", treeId = "tree_data", nodeList = [];
    function searchNode(newValue) {
        if (lastValue == newValue) {
            return;
        } else {
            lastValue = newValue;
        }
        var zTree = $.fn.zTree.getZTreeObj(treeId);
        updateNodes(false, nodeList, zTree);
        nodeList = zTree.getNodesByParamFuzzy("name", newValue);
        updateNodes(true, nodeList, zTree);

        var parents = new Set(nodeList);
        $.each(nodeList, function (i, v) {
            get_parents(v, parents);
            $.each(zTree.transformToArray(v), function (i1, v1) {
                parents.add(v1);
            });
        });
        var allNodes = zTree.transformToArray(zTree.getNodes());
        zTree.hideNodes(allNodes);
        parents.forEach(function (item, sameItem, parents) {
            zTree.showNode(sameItem);
        });
    }


    function get_parents(zTreeNode, parents) {
        if (zTreeNode != undefined) {
            parents.add(zTreeNode);
            var pNode = zTreeNode.getParentNode();
            get_parents(pNode, parents)

        }
    }


    function updateNodes(highlight, nodes, zTree) {
        for (var i = 0, l = nodes.length; i < l; i++) {
            nodes[i].highlight = highlight;
            zTree.updateNode(nodeList[i]);
        }
    }

    function getFontCss(treeId, treeNode) {
        return (!!treeNode.highlight) ? {color: "#A60000", "font-weight": "bold"} : {
            color: "#333",
            "font-weight": "normal"
        };
    }

</script>


<SCRIPT type="text/javascript">
    var setting = {
        check: {
            enable: true,
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            fontCss: getFontCss
        }
    };
    var nodes =${data};


    $(document).ready(function () {
        $.fn.zTree.init($("#tree_data"), setting, nodes);

        //展开第一级
        var treeObj = $.fn.zTree.getZTreeObj("tree_data");
        $.each(treeObj.getNodes(), function (i, v) {
            treeObj.expandNode(v, true, false, true);
        });

        //级联父节点
        $.each(treeObj.getCheckedNodes(true), function (i, v) {
            treeObj.checkNode(v, true, true);
        });

        $("#btn_search").click(function () {
            var newValue = $("#input_search").val();
            searchNode(newValue);
        });


    });
</SCRIPT>

<SCRIPT type="text/javascript">
    $("#btn_save_data").click(function () {
        var treeObj = $.fn.zTree.getZTreeObj("tree_data");
        var nodes = treeObj.getChangeCheckedNodes(), data = {
            insertLeafIds: [],
            deleteLeafIds: []

        };

        $.each(nodes, function (i, v) {
            if (!v.isParent) {
                if (v.checked) {
                    data.insertLeafIds.push(v.id);
                } else {
                    data.deleteLeafIds.push(v.id);
                }
            }
        });

        if (data.insertLeafIds.length + data.deleteLeafIds.length == 0) {
            $("#div_error").html($.sysPlugin.createAlert("未发现数据被修改"));
            return;
        }

        $.sysPlugin.superAjax({


            data: data,
            url: '${url}',
            operateType: '${operateType}',
            showSuccessAlert: true,
            showWarnAlert: true,
            successTitle:updateSuccessTitle,
            successCall: function (data) {
                $.sysPlugin.successCallBack(data, {
                    modal:$("#modal_ajax")
                });
                refreshTab($("#tab_expertMajor"));
            }
        });
    });
</SCRIPT>